package com.fourhats.services.impl;

import java.util.ArrayList;
import java.util.List;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;

import com.fourhats.dao.PostDAO;
import com.fourhats.model.Post;
import com.fourhats.services.BlogService;
import com.fourhats.services.dto.PostDto;

public class BlogServiceImpl implements BlogService {

	@Autowired
	private Mapper mapper;
	
	@Autowired
	private PostDAO postDAO;
	
	public List<PostDto> getAllOrderByDate() {
		return this.getDtos(this.postDAO.getAllOrderByDate());
	}

	public PostDto getById(Long postId) {
		return this.getDto(this.postDAO.getById(postId));
	}
	
	private List<PostDto> getDtos(List<Post> posts) {
		List<PostDto> dtos = new ArrayList<PostDto>();
		for(Post p : posts) {
			dtos.add(this.getDto(p));
		}
		
		return dtos;
	}
	
	private PostDto getDto(Post post) {
		return mapper.map(post, PostDto.class);
	}

}
